package com.cxh.mchat.common.config;

import cn.hutool.crypto.SmUtil;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.nio.charset.StandardCharsets;

public class SM4PasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        return SmUtil.sm4(SMProperties.sm4Key.getBytes(StandardCharsets.UTF_8)).encryptHex(charSequence.toString());
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        String plainText = SmUtil.sm4(SMProperties.sm4Key.getBytes(StandardCharsets.UTF_8)).decryptStr(s);
        return plainText.equalsIgnoreCase(charSequence.toString());
    }
}
